﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tauren.Business.DLL.System_Log;
using Tauren.Business.Entity.Config;
using Tauren.Business.Entity.Enum;
using Tauren.Business.Entity.Global;
using Tauren.Business.Entity.Model;
using Tauren.Business.Entity.Output;

namespace Tauren.Business.Service.System_Log
{
    /// <summary>
    /// 日志业务逻辑处理
    /// </summary>
    public class SystemLogService : ISystemLogService
    {
        public SystemLogService(ISystemLogBusiness business)
        {
            this.business = business;
        }

        public ISystemLogBusiness business { get; set; }

        public IResponseMessage Add(SystemLog Model) => business.Add(Model).Succeed();

        public IResponseMessage All() => business.All().Succeed();

        public IResponseMessage Pages(QueryModel queryModel)
        {
            TableListModel<SystemLogTable> tableListModel = new TableListModel<SystemLogTable>();
            List<TableListHeaderModele> Headers = new List<TableListHeaderModele>();
            Headers.Add(new TableListHeaderModele() { Key = "rowNo", Name = "序号", Width = 50, Align = "center", Fixed = true });
            Headers.Add(new TableListHeaderModele() { Key = "autoId", Name = "主键", Width = 90, Type = "primary_key", Align = "center", SorTable = SortType.custom.ToString(), Fixed = true, Filter = true });
            Headers.Add(new TableListHeaderModele() { Key = "account", Name = "账号", Width = 140, Type = "primary_name", Align = "center", IsOperation = true, SorTable = SortType.custom.ToString(), Fixed = true, Filter = true });
            Headers.Add(new TableListHeaderModele() { Key = "cnName", Name = "员工中文名", Width = 140, Align = "center", SorTable = SortType.custom.ToString(), Fixed = true, Filter = true });
            Headers.Add(new TableListHeaderModele() { Key = "enName", Name = "员工英文名", Width = 140, Align = "center", SorTable = SortType.custom.ToString(), Fixed = true, Filter = true });
            Headers.Add(new TableListHeaderModele() { Key = "empCode", Name = "员工编码", Width = 140, Align = "center", SorTable = SortType.custom.ToString(), Fixed = true, Filter = true });
            Headers.Add(new TableListHeaderModele() { Key = "tCnname", Name = "租户名称", Width = 180, Align = "center", IsTag = true, SorTable = SortType.custom.ToString(), Filter = true });
            Headers.Add(new TableListHeaderModele() { Key = "code", Name = "租户编码", Width = 120, Align = "center", IsTag = true, SorTable = SortType.custom.ToString(), Filter = true });
            Headers.Add(new TableListHeaderModele() { Key = "phone", Name = "租户联系方式", Width = 180, Align = "left", IsTag = true,});
            Headers.Add(new TableListHeaderModele() { Key = "interfacePath", Name = "接口地址", Width = 180, Align = "left" });
            Headers.Add(new TableListHeaderModele() { Key = "ipAddress", Name = "请求地址", Width = 180, Align = "left" });
            Headers.Add(new TableListHeaderModele() { Key = "serverAddress", Name = "接口服务器ip地址", Width = 180, Align = "left" });
            Headers.Add(new TableListHeaderModele() { Key = "language", Name = "语言环境", Width = 180, Align = "center", IsTag = true, Filter = true });
            Headers.Add(new TableListHeaderModele() { Key = "action", Name = "操作", Width = 160, Align = "center" });
            List<AccountListModel> employeeListModels = new List<AccountListModel>();
            long Total = 0;
            List<SystemLogTable> Data = business.Pages(queryModel, out Total);
            if (!(Data is null) && Data.Any())
            {
                Data.ForEach(o =>
                {
                    BuliderButtons(o.Operations, o, ButtomType.detaile);
                });
            }
            tableListModel.Body = Data;
            tableListModel.Total = Total;
            tableListModel.Header = Headers;

            return tableListModel.Succeed();
        }
        private void BuliderButtons(List<SystemButton> options, SystemLogTable item, ButtomType buttomType)
        {
            SystemButton btn = new SystemButton();
            btn.Label = "详情";
            btn.Value = buttomType.ToString();
            btn.Size = "mini";
            switch (buttomType)
            {

                case ButtomType.detaile:
                    btn.Icon = "el-icon-lx-attention";
                    break;
                default:
                    break;
            }
            btn.KeyFieldValue = item.AutoId.ToString();
            btn.PrimaryKeyValue = item.AutoId;
            options.Add(btn);
        }

        /// <summary>
        /// 字段模板
        /// </summary>
        /// <param name="PageType"></param>
        /// <returns></returns>
        public IResponseMessage Config()
        {
            List<OutPutConfig> Fields = new List<OutPutConfig>();

            OutPutConfig config = new OutPutConfig();
            config.Label = "请求地址";
            config.FieldKey = "ipAddress";
            config.MenuCode = "";
            config.ControlType = ControlType.text;
            config.Show = true;
            Fields.Add(config);
            config = new OutPutConfig();
            config.Label = "接口服务器ip地址";
            config.FieldKey = "serverAddress";
            config.MenuCode = "";
            config.ControlType = ControlType.text;
            config.Show = true;
            Fields.Add(config);
            config = new OutPutConfig();
            config.Label = "请求参数";
            config.FieldKey = "requestParamtetr";
            config.MenuCode = "";
            config.ControlType = ControlType.textarea;
            config.Show = true;
            Fields.Add(config);
            config = new OutPutConfig();
            config.Label = "客户端类型";
            config.FieldKey = "userAgent";
            config.MenuCode = "";
            config.ControlType = ControlType.textarea;
            config.Show = true;
            Fields.Add(config);
            config = new OutPutConfig();
            config.Label = "客户端地址";
            config.FieldKey = "clientPath";
            config.MenuCode = "";
            config.ControlType = ControlType.text;
            config.Show = true;
            Fields.Add(config);
            config = new OutPutConfig();
            config.Label = "接口地址";
            config.FieldKey = "interfacePath";
            config.MenuCode = "";
            config.ControlType = ControlType.text;
            config.Show = true;
            Fields.Add(config);
            config = new OutPutConfig();
            config.Label = "接口地址";
            config.FieldKey = "interfacePath";
            config.MenuCode = "";
            config.ControlType = ControlType.text;
            config.Show = true;
            Fields.Add(config);
            config = new OutPutConfig();
            config.Label = "执行时间";
            config.FieldKey = "executeDate";
            config.MenuCode = "";
            config.ControlType = ControlType.text;
            config.Show = true;
            Fields.Add(config);
            config = new OutPutConfig();
            config.Label = "完成时间";
            config.FieldKey = "executeCompletiontime";
            config.MenuCode = "";
            config.ControlType = ControlType.text;
            config.Show = true;
            Fields.Add(config);
            config = new OutPutConfig();
            config.Label = "执行类";
            config.FieldKey = "executeClass";
            config.MenuCode = "";
            config.ControlType = ControlType.text;
            config.Show = true;
            Fields.Add(config);
            config = new OutPutConfig();
            config.Label = "执行方法";
            config.FieldKey = "executeMethod";
            config.MenuCode = "";
            config.ControlType = ControlType.text;
            config.Show = true;
            Fields.Add(config);
            config = new OutPutConfig();
            config.Label = "执行参数";
            config.FieldKey = "executeParameter";
            config.MenuCode = "";
            config.ControlType = ControlType.textarea;
            config.Show = true;
            Fields.Add(config);
            config = new OutPutConfig();
            config.Label = "结果";
            config.FieldKey = "executeResult";
            config.MenuCode = "";
            config.ControlType = ControlType.textarea;
            config.Show = true;
            Fields.Add(config);
            return Fields.Succeed();
        }

        public IResponseMessage GetByID(long id)
        {
            return business.GetLog(id).OnSucceed();
        }
    }
}
